/*
 * @lc app=leetcode.cn id=482 lang=cpp
 *
 * [482] 密钥格式化
 */

// @lc code=start
class Solution
{
public:
  string licenseKeyFormatting(string s, int k)
  {
    string ans;
    for (int i = s.size() - 1, len = 0; i >= 0 ; --i) {
      if (s[i] == '-')
        continue;
      ans.push_back(toupper(s[i]));
      if (++len % k == 0)
        ans.push_back('-');
    }
    if (*(ans.rbegin()) == '-')
      ans.pop_back();
    reverse(ans.begin(), ans.end());
    return ans;
  }
};
// @lc code=end
